# -*- coding:utf-8 -*-
# @Time : 2020/11/25 11:40
# @Author: limeng
# @File : validate_params.py
import functools
from textwrap import wrap

from flask import request

from application import DevelopmentConfig
from application.libs import result_msg, const, result


def validate_params(params=None):
    """
    验证表单的验证器
    :param p_args: 必传参数集合
    :return:
    """
    if params is None:
        params = []

    def decorator(view_func):
        @functools.wraps(view_func)
        def inner(*args, **kwargs):
            if request.method == 'GET':
                form_data = request.args
            else:
                if request.json:
                    form_data = request.json
                else:
                    form_data = request.form
            if not form_data:
                return result_msg(const.FAIL_40000, f"参数为空")
            if not all(k in form_data for k in params):
                if DevelopmentConfig.DEBUG:
                    return result(const.FAIL_40000, f"参数错误,正确参数:{params}")
                else:
                    return result(const.FAIL_40000, '参数错误')  # 生产环境参数不进行输出
            return view_func(*args, **kwargs)

        return inner

    return decorator
